<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ClassifyModel;

class Classifycld extends Base
{
    // 子分类列表
    public function index()
    {
        $id = input('param.id');
        if(request()->isAjax()){
            $param = input('param.');
//            halt($param);
            $id = $param['id'];
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = ['pid'=>$id];
            if (!empty($param['searchText'])) {
                $where['name'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $classify = new ClassifyModel();
            $selectResult = $classify->getClassifyByWhere($where, $offset, $limit);
            foreach($selectResult as $key=>$vo){
//                $selectResult[$key]['thumbnail'] = '<img src="' . $vo['thumbnail'] . '" width="40px" height="40px">';
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            $return['total'] = $classify->getAllClassify($where);  // 总数据
            $return['rows'] = $selectResult;
            return json($return);
        }
            return $this->fetch('admin@classifycld/index',['id'=>$id]);
        }

    //编辑活动
//    public function classifySdy()
//    {
//        $classify = new ClassifyModel();
////            halt(request()->isPost());
//        if(request()->isPost()){
//
//            $param = input('post.');
//            unset($param['file']);
//            $flag = $classify->editClassify($param);
//
//            return json(msg($flag['code'], $flag['data'], $flag['msg']));
//        }
//        //根据id查询内容
//        $id = input('param.id');
//        $this->assign([
//            'article' => $classify->getOneclassify($id)
//        ]);
//        return $this->fetch();
//    }
    //子分类删除
    public function classifycldDel()
    {
        $id = input('param.id');
        $classifycld = new ClassifyModel();
        $flag = $classifycld->delClassify($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '删除' => [
                'auth' => 'articles/articledel',
                'href' => "javascript:classifycldDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}
